<?php

namespace app\api\controller\Course;

use app\api\controller\Common;
use app\api\Logic\Course\MallGoodsLogic;
use app\api\Logic\Course\ManagementPackageSublevelLogic;
use app\api\model\CampusCourseMallGoodsModel;
use think\exception\DbException;

/**
 * 选课接口
 */
class MallGoods extends Common
{
    protected $noNeedLogin = ['*'];

    protected $mallGoodsLogic;
    protected $managementPackageSublevelLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->mallGoodsLogic = new MallGoodsLogic();
        $this->managementPackageSublevelLogic = new ManagementPackageSublevelLogic();
    }

    /**
     * 选课列表
     * @throws DbException
     */
    public function getList(){
        if($this->request->isPost()){
            $params = $this->request->param();
            //验证数据
            $params = $this->verifyData($params);

            $query = CampusCourseMallGoodsModel::where('shop_id',$params['shop_id']);
            //根据类型筛选
            if(isset($params['type'])){
                $query = $query->where('type',$params['type']);
            }

            $list = $query->field('id,shop_id,type,name,c_id,image')
                ->where('status',1)
                ->order('create_time','desc')
                ->paginate(PAGE_SIZE);
            $list = objToArray($list) ?? [];

            if(!empty($list)){
                $this->success('查询成功',$list);
            }
            $this->error('暂无数据');
        }
        $this->error('请求错误');
    }

    /**
     * 获取选课详情
     */
    public function info(){
        if($this->request->isPost()){
            $params = $this->request->param();
            //验证数据
            $params = $this->verifyData($params);

            //选课ID
            $mallGoodsId = $params['mall_goods_id'] ?? 0;
            if(empty($mallGoodsId)){
                $this->error("选课ID必须");
            }

            //获取选项课程详情
            $info = $this->mallGoodsLogic->info($params['shop_id'],$mallGoodsId);
            if(empty($info)){
                $this->error('暂无数据');
            }

            if($info['status'] == 0){
                $this->error('该选课已下架');
            }

            $this->success('查询成功',$info);
        }
        $this->error('请求错误');
    }
}
